package com.ruoyi.basics.service.impl;

import com.ruoyi.basics.domain.WmsArea;
import com.ruoyi.basics.domain.WmsLocation;
import com.ruoyi.basics.domain.vo.WmsLocationVo;
import com.ruoyi.basics.mapper.WmsLocationMapper;
import com.ruoyi.basics.service.IWmsLocationService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 库位管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class WmsLocationServiceImpl implements IWmsLocationService 
{
    @Autowired
    private WmsLocationMapper wmsLocationMapper;

    /**
     * 查询库位管理
     * 
     * @param id 库位管理主键
     * @return 库位管理
     */
    @Override
    public WmsLocation selectWmsLocationById(Long id)
    {
        return wmsLocationMapper.selectWmsLocationById(id);
    }

    /**
     * 查询库位管理列表
     * 
     * @param wmsLocation 库位管理
     * @return 库位管理
     */
    @Override
    public List<WmsLocationVo> selectWmsLocationList(WmsLocation wmsLocation)
    {
        return wmsLocationMapper.selectWmsLocationList(wmsLocation);
    }

    /**
     * 新增库位管理
     * 
     * @param wmsLocation 库位管理
     * @return 结果
     */
    @Override
    public int insertWmsLocation(WmsLocation wmsLocation)
    {
        if (wmsLocation == null || StringUtils.isBlank(wmsLocation.getName())) {
            throw new IllegalArgumentException("区域名称不能为空");
        }
        // 2. 检查名称是否已存在（区分大小写）
        boolean exists = wmsLocationMapper.existsByAreaName(wmsLocation.getName());
        if (exists) {
            throw new RuntimeException("区域[" + wmsLocation.getName() + "]已存在");
        }


        // 先查询库区列表
        WmsArea WmsArea =  wmsLocationMapper.selectWmsLocationByAreaId(wmsLocation.getAreaId());
        if (WmsArea == null) {
            throw new RuntimeException("未找到ID为" + wmsLocation.getAreaId() + "的库区信息");
        }
        wmsLocation.setWarehouseId(WmsArea.getWarehouseId());

        //查询货主id
        Long ownerId = wmsLocationMapper.selectWmsOwnerById(wmsLocation.getOwnerId());
        if (ownerId == null) {
            throw new RuntimeException("未找到ID为" + wmsLocation.getOwnerId() + "的货主信息");
        }
        wmsLocation.setOwnerId(ownerId);


        wmsLocation.setCreateTime(DateUtils.getNowDate());
        return wmsLocationMapper.insertWmsLocation(wmsLocation);
    }


    /**
     * 修改库位管理
     * 
     * @param wmsLocation 库位管理
     * @return 结果
     */
    @Override
    public int updateWmsLocation(WmsLocation wmsLocation)
    {
        wmsLocation.setUpdateTime(DateUtils.getNowDate());
        return wmsLocationMapper.updateWmsLocation(wmsLocation);
    }

    /**
     * 批量删除库位管理
     * 
     * @param ids 需要删除的库位管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsLocationByIds(Long[] ids)
    {
        return wmsLocationMapper.deleteWmsLocationByIds(ids);
    }

    /**
     * 删除库位管理信息
     * 
     * @param id 库位管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsLocationById(Long id)
    {
        return wmsLocationMapper.deleteWmsLocationById(id);
    }

    /**
     * 库位状态修改
     * @param id
     * @param status
     * @return
     */
    @Override
    public int LocationStatus(Long id, Integer status) {
        // 检查ID是否有效
        if (id == null) {
            throw new IllegalArgumentException("库位ID不能为空");
        }
        // 查询库位信息
        WmsLocation wmsLocation = wmsLocationMapper.selectWmsLocationById(id);
        // 检查仓库是否存在
        if (wmsLocation == null) {
            throw new RuntimeException("未找到ID为" + id + "的库位信息");
        }

        // 切换状态
        wmsLocation.setStatus(wmsLocation.getStatus() == 1 ? 0 : 1);


        // 更新仓库状态
        return wmsLocationMapper.updateWmsLocation(wmsLocation);
    }

    /**
     * 生成库位编码
     * @return
     */
    @Override
    public String generateLocationCode() {
        String maxCode = wmsLocationMapper.findMaxLocationCode();
        String prefix = "KW";
        Pattern pattern = Pattern.compile(prefix + "(\\d+)");
        int newCount = 1;
        if (maxCode != null) {
            Matcher matcher = pattern.matcher(maxCode);
            if (matcher.find()) {
                String numberPart = matcher.group(1);
                newCount = Integer.parseInt(numberPart) + 1;
            }
        }
        // 格式化数字部分为 6 位，不足补零
        return prefix + String.format("%06d", newCount);
    }

    @Override
    public int insertWmsLocations(List<WmsLocationVo> wmsLocationVos) {
        return wmsLocationMapper.insertWmsLocations(wmsLocationVos);
    }
}
